<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2021/1/26
 * Time: 15:15
 * 区域管理
 */

namespace app\admin\controller;


use think\Db;
use think\Request;

class District extends Base
{
    /**
     * 区域列表
     */
    public function index(){
        if(request()->isAjax ()){
            $keyword = input('keyword');
            
            $where = [];
            if(isset($keyword) && $keyword != ''){
                $where['dis_admin_name|dis_name|dis_id'] = ['like', '%' . $keyword . '%'];
            }
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::table('up_district')->where($where)->count();// 获取总条数
            $lists = Db::table('up_district')
                ->field('dis_id, dis_admin_name, dis_name, dis_time')
                ->where($where)
                ->order('dis_time desc')
                ->page($Nowpage,$limits)
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    
    /**
     * 添加区域
     * @return mixed|\think\response\Json
     */
    public function districtAdd(){
        if(Request::instance()->isPost()){
            $dis_name = trim(input('dis_name'), ' ');
            $dis_admin_ids = input('dis_admin_ids');
            $admin_name = Db::table('up_admin')->where('id', 'IN', $dis_admin_ids)->column('real_name');
            $insert = [
                'dis_admin_ids' => ',' . $dis_admin_ids . ',',
                'dis_admin_name' => implode(',', $admin_name),
                'dis_name' => $dis_name,
                'dis_time' => date('Y-m-d H:i:s')
            ];
            
            $res = Db::table('up_district')->insert($insert);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        $admin = Db::table('up_admin')
            ->where('status', 1)
            ->where('id', 'NEQ', 1)
            ->field('id, username, real_name')
            ->select();
        return $this->fetch('district/districtadd', ['admin' => $admin]);
    }
    
    /**
     * 编辑区域
     * @return mixed|\think\response\Json
     */
    public function districtEdit(){
        $dis_id = input('dis_id');
        if(Request::instance()->isPost()){
            $dis_name = trim(input('dis_name'), ' ');
            $dis_admin_ids = input('dis_admin_ids');
            $admin_name = Db::table('up_admin')->where('id', 'IN', $dis_admin_ids)->column('real_name');
            $update = [
                'dis_admin_ids' => ',' . $dis_admin_ids . ',',
                'dis_admin_name' => implode(',', $admin_name),
                'dis_name' => $dis_name,
                'dis_updatetime' => date('Y-m-d H:i:s')
            ];
    
            $res = Db::table('up_district')->where('dis_id', $dis_id)->update($update);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $district = Db::table('up_district')
            ->where('dis_id', $dis_id)
            ->field('dis_admin_ids, dis_name')
            ->find();
        $district['dis_admin_ids'] = trim($district['dis_admin_ids'], ',');
        $admin = Db::table('up_admin')
            ->where('status', 1)
            ->where('id', 'NEQ', 1)
            ->field('id, username, real_name')
            ->select();
        $this->assign([
            'district' => $district,
            'admin' => $admin,
            'dis_id' => $dis_id
        ]);
        return $this->fetch('district/districtedit');
    }
    
    /**
     * 删除区域
     * @return \think\response\Json
     */
    public function disDel(){
        $dis_id = input('id');
        $res = Db::table('up_district')->delete($dis_id);
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
}